package com.stormrage.edu.serv.ent.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.ent.EntAcctDao;
import com.stormrage.edu.serv.ent.EntAcctServ;
import com.stormrage.entity.ent.EntAcct;
import com.stormrage.entity.ent.rep.EntAcctRep;
import com.stormrage.entity.ent.req.EntAcctReq;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Service
public class EntAcctServImpl implements EntAcctServ {

	private static final Logger logger = LoggerFactory.getLogger(EntAcctServImpl.class);

	@Autowired
	private EntAcctDao entAcctDao;

	@Override
	public void updateLock(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 UpdateLock 】-【 STARTED 】");

		EntAcct acct = queryById(arg0);

		Integer locked = acct.getLocked() == 0 ? 1 : 0;
		acct.setLocked(locked);

		entAcctDao.updateLock(acct);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 UpdateLock 】-【 COMPLETED 】");

	}

	@Override
	public void updatePass(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 updatePass 】-【 STARTED 】");

		EntAcct acct = queryById(arg0);

		acct = EntAcct.instance(acct);

		entAcctDao.updatePass(acct);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 updatePass 】-【 COMPLETED 】");

	}

	@Override
	public EntAcct queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryListByEntId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EntAcct entity = entAcctDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryListByEntId 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<EntAcctRep> queryListByEntIdLock(final EntAcctReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryListByEntId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("entId", arg0.getEntId());
		map.put("locked", arg0.getLocked());

		List<EntAcct> array = entAcctDao.queryListByEntIdLock(map);

		List<EntAcctRep> list = new ArrayList<EntAcctRep>();
		Integer seq = 1;

		Iterator<EntAcct> it = array.iterator();
		while (it.hasNext()) {
			EntAcct acct = it.next();
			EntAcctRep rep = EntAcctRep.instance(acct, seq);
			list.add(rep);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryListByEntId 】-【 COMPLETED 】");
		return list;

	}

	@Override
	public List<EntAcctRep> queryListForPass() throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryListByEntId 】-【 STARTED 】");

		List<EntAcctRep> array = entAcctDao.queryListForPass();

		Integer seq = 1;

		Iterator<EntAcctRep> it = array.iterator();
		while (it.hasNext()) {
			EntAcctRep rep = it.next();
			rep = EntAcctRep.instance(rep, seq);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryListByEntId 】-【 COMPLETED 】");
		return array;

	}

}
